package com.atguigu.controller;

import com.atguigu.bean.Department;
import com.atguigu.bean.Employee;
import com.atguigu.dao.DepartmentDao;
import com.atguigu.dao.EmployeeDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.support.DaoSupport;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.Collection;

/**
 * @author 伟哥
 * @date 2021-05-17 16:05
 */
@Controller
public class EmployeeController {

    @Autowired
    EmployeeDao employeeDao;

    @Autowired
    DepartmentDao departmentDao;

    /**
     * 处理首页index.jsp的 /emps请求
     *      请求方式：Get
     * @return 查询所有员工
     */
    @RequestMapping("/emps")
    public String getEmps(Model model){
        // 1、使用dao查询出所有的员工
        Collection<Employee> all = employeeDao.getAll();
        // 2、将信息存储在request域中，由页面展示
        model.addAttribute("emps",all);
        return "list";
    }

    /**
     * 增加员工：
     *      查询出所有的部门信息，用于页面展示
     * @param model
     * @return
     */
    @RequestMapping("/toAddPage")
    public String toAddPage(Model model){
        // 1、查询部门信息，用于回显
        Collection<Department> departments = departmentDao.getDepartments();
        model.addAttribute("depts",departments);
        /* 默认：提前在隐含模型中，添加command用于SpringMVC【表单标签】回显 */
        model.addAttribute("command",new Employee(null,"张三","123@123.com",1,departmentDao.getDepartment(105)));

        /* 手动指定: deptDefault于SpringMVC的【表单标签】进行绑定，new 一个空的Employee对象*/
        model.addAttribute("deptDefault",new Employee());
        return "add";
    }

    /**
     * 保存员工
     *      规定请求方式为：POST
     * @return
     */
    @RequestMapping(value = "/emp",method = RequestMethod.POST)
    public String addEmployee(Employee employee){
        System.out.println("要添加的员工："+employee);

        // 存储员工
        employeeDao.save(employee);
        /*
         返回列表页面
         return "index";

            但是这样，来到index界面，会重新发送/emps
            这里我们，直接重定向，来到/emps
         */
        return "redirect:/emps";
    }

    /**
     * 编辑Edit员工
     * @param id
     * @return
     */
    @RequestMapping(value = "/emp/{id}",method = RequestMethod.GET)
    public String getEmp(@PathVariable("id") Integer id,Model model){
        // 1、查出员工信息
        Employee employee = employeeDao.get(id);
        // 2、放在请求域中
        model.addAttribute("employee",employee);

        // 3、查询部门信息，放在隐含模型中，用来回显，edit页面的，部门下拉列表
        Collection<Department> departments = departmentDao.getDepartments();
        model.addAttribute("depts",departments);
        return "edit";
    }

    /**
     * 更新员工的方法
     * @param employee
     * @return
     */
    @RequestMapping(value = "/emp/{id}",method = RequestMethod.PUT)
    public String updateEmployee(@ModelAttribute("employee") Employee employee){
        System.out.println(employee);
        employeeDao.save(employee);
        return "redirect:/emps";
    }

    /**
     * 提前运行：查询员工
     */
    @ModelAttribute
    public void peekEmployee(@RequestParam(value = "id",required = false)Integer id,Model model){
        System.out.println("ModelAttribute标注的peekEmployee方法运行了...");
        if (id != null){
            Employee employee = employeeDao.get(id);
            model.addAttribute("employee",employee);
        }
    }

    /**
     * 删除员工
     */
    @RequestMapping(value = "/emp/{id}",method = RequestMethod.DELETE)
    public String deleteEmploy(@PathVariable("id") Integer id){
        employeeDao.delete(id);

        return "redirect:/emps";
    }
}
